/*
 * @Author: liaoshuang 345324985@qq.com
 * @Date: 2023-03-22 13:29:19
 * @LastEditors: liaoshuang 345324985@qq.com
 * @LastEditTime: 2023-12-29 16:35:45
 * @FilePath: \threejstype\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/views/login/index.vue";
Vue.use(VueRouter);

export const router = new VueRouter({
  // history: VueRouter.createWebHashHistory(),
  routes: [
    {
      path: "/",
      name: "Home",
      component: () => import("../views/home/index.vue"),
      meta: {
        title: "Home",
      },
    },
    {
      path: "/transfer",
      name: "transfer",
      component: () => import("../views/transfer/index.vue"),
      meta: {
        title: "Transfer",
      },
    },
    {
      path: "/shopFloor",
      name: "ShopFloor",
      component: () => import("../views/shopFloor/index.vue"),
      meta: {
        title: "shopFloor",
      },
    },
    {
      path: "/shopFloorItem",
      name: "ShopFloorItem",
      component: () => import("../views/shopFloor/item.vue"),
      meta: {
        title: "ShopFloorItem",
      },
    },
    {
      path: "/login",
      name: "Login",
      component: () => import("../views/login/index.vue"),
      meta: {
        title: "Login",
      },
    },
  ],
});

router.beforeEach((to, from, next) => {
  window.scrollTo({
    top: 0,
    behavior: "smooth", // 平滑滚动
  });
  sessionStorage.setItem("currentPath", to.path);

  const isAuthenticated = sessionStorage.getItem("token");
  if (to.name === "Login" && isAuthenticated) {
    next("/");
  } else if (to.name !== "Login" && !isAuthenticated) {
    next({ name: "Login" });
  } else {
    next();
  }
});
